/**
 * Created by yongwang on 2015/9/16.
 */

var async = require('async');
var mysqlUtil = require('../../../common/utils/mysqlUtil');

exports.integralStrategyServiceList = function(req ,callback) {

    var body = req.body;
    var page = req.params.page;
    var size = Number(req.params.size);
    var index = (Number(req.params.page) - 1) * size;
    var totalPage = 0;

    var sql1 = "select count(*) as count from score_strategy_v2 ";
    var sql2 = "select * from score_strategy_v2  limit " + index +"," + size;

    logger.debug(body);

    if (body.name) {
        sql1 = sql1 + " where item_name like '%" + body.name + "%'";
        sql2 = "select * from score_strategy_v2  where item_name like '%" + body.name +"%' limit " + index +"," + size;
    }
    logger.debug(sql1);
    logger.debug(sql2);
    async.series([

        function (callbackInner) {
            mysqlUtil.query(sql1, function (err, result) {

                if (err || result.length == 0) {
                    callbackInner(err, null);
                }else {
                    var count = result[0].count;
                    totalPage = Math.ceil(count/size);
                    var temp = {count: count, totalPage:totalPage};


                    if (page > totalPage) {
                        page = totalPage;
                        index = (Number(page) - 1) * size;
                        sql2 = "select * from score_strategy_v2  limit " + index +"," + size;
                    }

                    logger.debug(temp);
                    callbackInner(err, temp);
                }

            });
        },

        function (callbackInner) {
            mysqlUtil.query(sql2, function (err, result) {
                callbackInner(err, result);
            });
        }

    ], callback);
};

exports.integralStrategyAddService = function(req ,callback){
    var body = req.body;
    if(body.item_type&&Number(body.item_type)!=NaN)body.item_type = Number(body.item_type);
    if(body.item_value&&Number(body.item_value)!=NaN)body.item_value = Number(body.item_value);
    if(body.item_value_limit_day&&Number(body.item_value_limit_day)!=NaN)body.item_value_limit_day = Number(body.item_value_limit_day);
    if(body.item_value_limit_month&&Number(body.item_value_limit_month)!=NaN)body.item_value_limit_month = Number(body.item_value_limit_month);
    body.update_time = new Date().getTime();

    var sql = "insert into score_strategy_v2  set ?";

    mysqlUtil.query(sql, [body],function (err, result) {
        callback(err, result);
    });
};

exports.integralStrategyDeleteService = function(req ,callback){
    var body = req.body;
    if (!body.integralStrategyID) {
        callback({domain:"û��id"}, null);
    }

    var temp = "(";
    for(var i = 0, leng = body.integralStrategyID.length;i<leng;i++) {
        if (i == leng - 1) temp = temp + body.integralStrategyID[i];
        else temp = temp + body.integralStrategyID[i] +",";
    }
    temp += ")";

    var sql = "delete from score_strategy_v2  where id in " + temp;
    mysqlUtil.query(sql, callback);
};

exports.integralStrategyEditService = function(req ,callback){
    var body = req.body;

    if (!body.integralStrategyID) {
        callback("û��id", null);
    }else {
        body.integralStrategyID = Number(body.integralStrategyID);
        var sql = "update score_strategy_v2  set ? where id = ?";
        mysqlUtil.query(sql, [body.integralStrategy, body.integralStrategyID],function (err, result) {
            callback(err, result);
        });
    }
};